package com.cn.demo4.controller;

import com.alibaba.fastjson.JSONObject;
import com.cn.demo4.entity.UserEntity;
import com.cn.demo4.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
public class LoginController{
    @Autowired
    private IUserService iUserService;
    @RequestMapping("/login")
    @ResponseBody
    public void login(UserEntity userEntity, HttpServletResponse resp, HttpServletRequest req) throws IOException {
        String sessionCode = (String) req.getSession().getAttribute("code");
        userEntity.setSessionCode(sessionCode);
        String result = iUserService.login(userEntity);
        String phone = userEntity.getPhone();
        String str = "success";
        if(str.equals(result)){
            req.getSession().setAttribute(phone,phone);
        }
        UserEntity model = iUserService.findByPhone(phone);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result",result);
        jsonObject.put("model",model);
        resp.getWriter().println(jsonObject);
    }

}
